﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FortuneHeaven.Entity.NetBar;
using Jzy.Core.Utility;
using FortuneHeaven.BLL.NetBar;
using Jzy.Core.Common.Basic;
using Jzy.Core.Common.Web;
using Jzy.Core.Common;
using Jzy.Common;
using Jzy.Core.Admin;

namespace FortuneHeaven.Admin.Pages.NetBar
{
    public partial class UserList : PageList
    {
       
        private OperationMode Operation
        {
            get
            {
                return (OperationMode)int.Parse(this.Decrypt(Request.QueryString["OperationMode"]));
            }
        }

        private UserExBO UserEx
        {
            get
            {
                return UserExBO.Instance<UserExBO>();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {                
                BindView();
            }
            base.InitSelectID(hidSelectIDs);
            AddAttr();
        }

        private void BindView()
        {
            LVUtil.Init(lv, lvHeader);
            PagerSetting setting = new PagerSetting();
            setting.PageIndex = this.PageIndex;
            setting.PageSize = AppConfig.PageSize;
            setting.OrderBy = "InputTime desc";
            if (txtName.Text.Trim() != "")
            {
                setting.Where = "ID in (select ID from [user] where Name like @p0 or LoginName like @p0)";
            }
            if (Operation == OperationMode.AddAdmin)
            {
                UserExInfo bossInfo = UserEx.GetInfo(AppSession.UserID);
                if (!string.IsNullOrEmpty(setting.Where))
                {
                    setting.Where += " and ";
                }
                setting.Where += " NetBarID=" + bossInfo.NetBarID;
            }


            string prefix = GlobalVar.WebCurrPath + "List.aspx?OperationMode=" + this.Encrypt((int)Operation) + "&Name=" + Server.UrlEncode(txtName.Text);
            List<UserExInfo> list = this.UserEx.GetList(setting, "%" + txtName.Text + "%");
            lv.DataKeyNames = new string[] { "ID" };
            lv.DataSource = list;
            lv.DataBind();

            litPager.Text = HtmlPager.Render(setting, prefix, true);

        }

        private void AddAttr()
        {
            JSUtil.ShowForm(lnkAdd, "增加用户", null, GlobalVar.WebCurrPath + "UserEdit.aspx?OperationMode=" + this.Encrypt((int)this.Operation), 300, 201);
            JSUtil.ClickBtn(lnkDelete, btnDelete);
            JSUtil.ClickBtn(lnkStartup, btnStartup);
            JSUtil.ClickBtn(lnkStop, btnStop);
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            BindView();
        }

        protected void lv_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                LVUtil.SetEdit(sender, e, 300, 201, GlobalVar.WebCurrPath + "UserEdit.aspx?OperationMode=" + this.Encrypt((int)this.Operation));

            }
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            BindView();
        }
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (strSelectIDs != "")
            {
                UserBO user = new UserBO();

                user.Delete(StrUtil.SplitToStr(strSelectIDs));
            }
            BindView();
        }
        protected void btnStartup_Click(object sender, EventArgs e)
        {
            List<int> lstMemberIDs = StrUtil.SplitToInt(strSelectIDs);
            //this.NetBar.Startup(lstMemberIDs);
            BindView();
        }

        protected void btnStop_Click(object sender, EventArgs e)
        {
            List<int> lstMemberIDs = StrUtil.SplitToInt(strSelectIDs);
            // NetBar.Stop(lstMemberIDs);
            BindView();
        }
    }
}